package cn.org.timespace.ons.action.base;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.ons.service.base.OptionsService;

import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.util.pager.PagerHelper;
import cn.org.timespace.ons.util.web.Security;
import cn.org.timespace.ons.vo.base.Options;

/*
 * 公共下拉选项管理
 * author:ync
 */
public class OptionsAction extends EFBaseAction{

	private static final long serialVersionUID = -3416183313291298070L;
	private static Log log=LogFactory.getLog(OptionsAction.class);
	private OptionsService tsOptionsService;
	
	public OptionsAction()
	{
		super(new OptionsModel());
	}
	OptionsModel optionsModel=(OptionsModel)this.getModel();
	
	public String doList() throws Exception{
		
		String code = getRequest().getParameter("code") ;
		optionsModel.setCode(code);
		Pager p = PagerHelper.getPagerInstance(getRequest());
		List<Object> resList = tsOptionsService.findByCondition(optionsModel,p);
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		//保存通过url传过来的code参数
		getRequest().setAttribute("code", code);
		
		return LIST;
	}
	public String doUpdate() throws Exception {
		
		Options options = optionsModel.getOptions();
		
		tsOptionsService.update(options);
		
		return doList();
	}
	
	public String doSave() throws Exception{
		Options options = optionsModel.getOptions();
		
		tsOptionsService.save(options);
		
		return ADD;
	}
	
	public String doDelete() throws Exception{

		String theId = getRequest().getParameter("theId") ;
		Options options = tsOptionsService.findById(theId);
		tsOptionsService.delete(options);
		
		return doList();
	}
	
	public String doFindById() throws Exception{
		String theId = getRequest().getParameter("theId") ;
		Options options = new Options();
		options.setCode(getRequest().getParameter("code"));
		if(!StringUtils.isEmpty(theId)){  //更新
		    options = tsOptionsService.findById(theId);
		    optionsModel.setOptions(options);
			return UPDATE;
		}
		optionsModel.setOptions(options);
		return ADD;
		
	}
	
	/**
	 * 校验用户账号是否存在
	 * @return 存在返回 true 不存在返回false
	 * @throws Exception
	 */
	public String doCheckOptionsNameIsExist() throws Exception{
		String result = "true"; //存在
		String name = getRequest().getParameter("name");
		Options options = tsOptionsService.doCheckOptionsNameIsExist(name);
		if(options == null){
			result = "false"; //不存在
		}
		
		getResponse().setCharacterEncoding("UTF-8");
		PrintWriter out = getResponse().getWriter();
		out.write(result);
		
		return null;
	}
	public OptionsService getTsOptionsService() {
		return tsOptionsService;
	}
	public void setTsOptionsService(OptionsService tsOptionsService) {
		this.tsOptionsService = tsOptionsService;
	}
	

}
